package pers.sloera.typing.frame

import java.awt.Container
import java.awt.event.KeyEvent
import java.awt.event.KeyListener
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import java.time.LocalDateTime
import java.util.*
import javax.swing.*
import kotlin.collections.HashMap

/**
 * class pers.sloera.typing.frame
 * user sloera
 * date 8/9/21
 */
class Window : JFrame, KeyListener, MouseListener {
    val letterStatisc = HashMap<Int, Int>(60)

    var con: Container? = null
    lateinit var jButton: Array<Array<JButton>>
    var jp: JPanel? = null
    var textArea: JTextArea? = null
    var reset: JButton? = null
    private val keys = arrayOf("1234567890-=", "QWERTYUIOP[]", "ASDFGHJKL'", "ZXCVBNM,./")

    constructor(title: String?) : super(title) {
        println("with string")
    }

    constructor() : super("打字练习") {
        this.draw()
    }


    fun draw() {
        var exampleTextField = JTextField("示例文本")
        exampleTextField.setBounds(50, 50, 150, 150)
        exampleTextField.isEditable = false
        val typeTextField = JTextField()
        typeTextField.addKeyListener(this)
        typeTextField.setBounds(50, 200, 150, 200)
        this.add(exampleTextField)
        this.add(typeTextField)
        this.setSize(300, 300)
        this.setLocationRelativeTo(null)
        this.setLayout(null)
        this.setVisible(true)
    }


    override fun keyTyped(e: KeyEvent?) {
        // println(e!!.keyCode)
        if (e != null) {
            println(e.keyChar)
            println(LocalDateTime.now())
        }
        return
        TODO("Not yet implemented")
    }

    override fun keyPressed(e: KeyEvent?) {
        // println(e!!.keyCode)
        // println(e.keyChar)
        return
        TODO("Not yet implemented")
    }

    override fun keyReleased(e: KeyEvent?) {
        // println(e!!.keyCode)
        // println(e.keyChar)
        if (e != null) {
            letterStatisc[e.keyCode] = Optional.ofNullable(letterStatisc[e.keyCode]).orElse(0) + 1
        }
        return
        TODO("Not yet implemented")
    }

    override fun mouseClicked(e: MouseEvent?) {
        println(e)
        return
        TODO("Not yet implemented")
    }

    override fun mousePressed(e: MouseEvent?) {
        println(e)
        return
        TODO("Not yet implemented")
    }

    override fun mouseReleased(e: MouseEvent?) {
        println(e)
        return
        TODO("Not yet implemented")
    }

    override fun mouseEntered(e: MouseEvent?) {
        println(e)
        return
        TODO("Not yet implemented")
    }

    override fun mouseExited(e: MouseEvent?) {
        println(e)
        return
        TODO("Not yet implemented")
    }
}